﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KpBusiness.Model;
using KpBusiness.Service;
using WebApp.Utility;
using System.Configuration;
using System.Web.Security;
using KpMis.Utility;

namespace KpMis.Controllers
{ 
    public class UserInformationController : Controller
    {
        

        //
        // GET: /UserInformation/

        public ViewResult Index()
        {
            
            return View(new UserInformationService().GetAllUsersInformation());
        }

        //
        // GET: /UserInformation/Details/5

        public ViewResult Details(int id)
        {
            UserInformation userinformation = new UserInformationService().GetUserInformation(id);
            return View(userinformation);
        }

        //
        // GET: /UserInformation/Create

        public ActionResult Create()
        {
            ViewBag.Roles = new RoleService().GetAllRoles();
            return View();
        } 

        //
        // POST: /UserInformation/Create

        [HttpPost]
        [ValidateOnlyIncomingValuesAttribute]
        public ActionResult Create(UserInformation userinformation)
        {
            ///Check confirm password
            if (userinformation.Password != userinformation.ConfirmPassword)
            {
                ModelState.AddModelError("Password", "ConfirmPassword Do not match with password");
            }

            ///Check if user already exists
            UserInformation existingUser = new UserInformationService().GetUserInformationByUserName(userinformation.UserName);
            if (existingUser != null)
            {
                ModelState.AddModelError("UserName", "UserName already exists please provide another username");
            }

            if (ModelState.IsValid)
            {
                userinformation.RegistrationDate = DateTime.Now;
                new UserInformationService().CreateUserInformation(userinformation);
                
                return RedirectToAction("Index");  
            }
            ViewBag.Roles = new RoleService().GetAllRoles();
            return View(userinformation);
        }


        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        [ValidateOnlyIncomingValuesAttribute]
        public ActionResult Register(UserInformation userinformation)
        {
            ///Check confirm password
            if (userinformation.Password != userinformation.ConfirmPassword)
            {
                ModelState.AddModelError("Password", "ConfirmPassword Do not match with password");
            }

            ///Check if user already exists
            UserInformation existingUser = new UserInformationService().GetUserInformationByUserName(userinformation.UserName);
            if (existingUser != null)
            {
                ModelState.AddModelError("UserName", "UserName already exists please provide another username");
            }

            if (ModelState.IsValid)
            {
                userinformation.RegistrationDate = DateTime.Now;
                Role role = new Role();
                role.RoleId = int.Parse(ConfigurationManager.AppSettings["CanidateRoleId"]);
                userinformation.Role = role;
                new UserInformationService().CreateUserInformation(userinformation);
                return RedirectToAction("Index");
            }
            
            return View(userinformation);
        }

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();

            return RedirectToAction("Index", "Home");
        }

        public ActionResult Login()
        {
            return View();
        }


        [HttpPost]
        [ValidateOnlyIncomingValuesAttribute]
        public ActionResult Login(UserInformation userinformation, string returnUrl)
        {
            UserInformation validatedUser = new UserInformationService().ValidateUserInformation(userinformation.UserName, userinformation.Password);
            if (validatedUser == null)
            {
                ModelState.AddModelError("UserId", "Please provide valid userid and password");
            }

            if (ModelState.IsValid)
            {
                FormsAuthentication.SetAuthCookie(userinformation.UserName, false);


                Session[ConstantMaster.UserSessionString] = validatedUser;


                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                       && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
                
            }
            return View(userinformation);
        }
        


        //
        // GET: /UserInformation/Edit/5
 
        public ActionResult Edit(int id)
        {
            UserInformation userinformation = new UserInformationService().GetUserInformation(id);
            ViewBag.Roles = new RoleService().GetAllRoles();
            return View(userinformation);
        }

        //
        // POST: /UserInformation/Edit/5

        [HttpPost]
        [ValidateOnlyIncomingValuesAttribute]
        public ActionResult Edit(UserInformation userinformation)
        {
            ///Check confirm password
            if (userinformation.Password != userinformation.ConfirmPassword)
            {
                ModelState.AddModelError("Password", "ConfirmPassword Do not match with password");
            }


            if (ModelState.IsValid)
            {
                new UserInformationService().UpdateUserInformation(userinformation);
                return RedirectToAction("Index");
            }
            ViewBag.Roles = new RoleService().GetAllRoles();
            return View(userinformation);
        }

        //
        // GET: /UserInformation/Delete/5
 
        public ActionResult Delete(int id)
        {
            UserInformation userinformation = new UserInformationService().GetUserInformation(id);
            return View(userinformation);
        }

        //
        // POST: /UserInformation/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
 
            base.Dispose(disposing);
        }
    }
}